<?php
// +----------------------------------------------------------------------
// | CLTPHP.Pro [后台日志管理]
// +----------------------------------------------------------------------
// | Copyright (c) 2019-2029 https://www.cltphp.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( https://www.cltphp.com/detail-140.html )
// +----------------------------------------------------------------------
// | Author: chichu <1109305987@qq.com>
// +----------------------------------------------------------------------
namespace app\admin\controller;

use app\admin\model\AdminLog as M;
use think\facade\Db;
use think\facade\Request;

class AdminLog extends Base
{
    // 列表
    public function index(){
        if(Request::isAjax()) {
            //全局查询条件
            $page = input('page') ? input('page') : 1;
            $pageSize = input('limit') ? input('limit') : config('pageSize');
            $page = ['list_rows' => $pageSize, 'page' => $page];

            $key = input('post.key');
            $where = [];
            if (!empty($key)) {
                $where[] = ['username|title', 'like', '%' . $key . '%'];
            }
            //非超级管理员只能查看自己的日志
            if (session('admin.id') > 1) {
                $where[] = ['admin_id', '=', session('admin.id')];
            }
            //调取列表
            $list = M::getList($where, $page, ['id' => 'desc'])->toArray();
            return ['code' => 0, 'msg' => '获取成功!', 'data' => $list['data'], 'count' => $list['total'], 'rel' => 1];
        }else{
            return view();
        }
    }
    // 删除
    public function del($id){
        if($id){
            M::destroy($id);
        }else{
            Db::name('admin_log')->delete(true);
        }
        return ['code'=>0,'msg'=>'删除成功!'];
    }
}
